//
//  RJMainSuperVCViewController.m
//  AirQuality
//
//  Created by Po on 15/3/10.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJMainSuperVC.h"
@interface RJMainSuperVC ()

- (void)initData;
- (void)initSuperInterface;
@end

@implementation RJMainSuperVC
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSuperInterface];
    [self.view setFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
}

#pragma mark - user-defined initialization function
- (void)initData
{
    
}

- (void)initSuperInterface
{
    [self.view addSubview:[self getBackGourndImage]];
    [self.view addSubview:[self getNavBar]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view setClipsToBounds:YES];
}


- (UIImageView *)getBackGourndImage
{
    if (!_backgroundImage) {
        _backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];

    }
    return _backgroundImage;
}

- (RJNavBar *)getNavBar
{
    if (!_navBar) {
        _navBar = [[RJNavBar alloc] init];
    }
    return _navBar;
}

- (UIView *)mainView
{
    if (!_mainView) {
        _mainView = [[UIView alloc] initWithFrame:CGRectMake(0, sysNavBarHeight, ScreenWidth, ScreenHeight - sysNavBarHeight - sysTabbarHeight)];
        [self.view insertSubview:_mainView belowSubview:_navBar];
    }
    return _mainView;
}

#pragma mark - function

- (void)playMoveAnimDirection:(moveDirection)direction isRemove:(BOOL)isRemove
{
    //释放上一个页面的操作
    if (isRemove) {
        if (_delegate && [_delegate respondsToSelector:@selector(RJMainSuperVCWillFreeAll)]) {
            [_delegate RJMainSuperVCWillFreeAll];
        }
    }
    //切换VC动画
    [UIView animateWithDuration:(isRemove ? 0.7 : 0.5) delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.view setFrame:(
                             isRemove ?
                             [self checkMoveRect:direction] :
                             CGRectMake(0, 0, ScreenWidth, ScreenHeight))];
    } completion:^(BOOL finished) {
        if (isRemove) {
            [self removeFromParentViewController];
            [self.view removeFromSuperview];
            [self freeData];
        }
    }];
}

//获取移动的位置
- (CGRect)checkMoveRect:(moveDirection)direction
{
    CGRect rect = self.view.frame;
    CGFloat moveX = rect.origin.x;                //移动后的位置
    CGFloat moveY = rect.origin.y;
    switch (direction) {
        case moveUp:
            break;
        case moveDown:
            break;
        case moveLeft:
            moveX = rect.origin.x - ScreenWidth;
            break;
        case moveRight:
            moveX = rect.origin.x + ScreenWidth;
            break;
        default:
            break;
    }
    return CGRectMake(moveX, moveY, rect.size.width, rect.size.height);
}

- (void)freeData
{
    if (_delegate) {
        [_delegate RJMainSuperVCFreeAll];
    }
}
@end
